<?php
// 生成添加医生表单
// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");

// 生成随机工号
$selectDocID = "SELECT doc_ID FROM doc_infos";
$resultDocID = mysqli_query($connect_DB, $selectDocID);
if ($resultDocID) {
    $docIDs = array();
    while ($row = mysqli_fetch_assoc($resultDocID)) {
        $docIDs[] = $row['doc_ID'];
    }
}

$selectDelID = "SELECT doc_ID FROM history_doc_infos";
$resultDelID = mysqli_query($connect_DB, $selectDelID);
if ($resultDelID) {
    $delIDs = array();
    while ($row = mysqli_fetch_assoc($resultDelID)) {
        $delIDs[] = $row['doc_ID'];
    }
}

function random_str($length, $type = 'num') {
    $key = '';
    for ($i = 0; $i < $length; $i++) {
        if ($type === 'num') {
            $key .= chr(rand(48, 57));
        } elseif ($type === 'str') {
            $key .= chr(rand(65, 90));
        }
    }
    return $key;
}

$doc_ID = "doc" . random_str(5);

while (true) {
    if (in_array($doc_ID, $docIDs) || in_array($doc_ID, $delIDs)) {
        $doc_ID = "doc" . random_str(5);
    }else{
        break;
    }
}


echo "
<form class='row g-3 form-div' action='a_SubmitAddDoctor.php' method='post' enctype='multipart/form-data'>
    <h4>添加医生信息</h4>
    <div class='col-8'>
        <label for='docID' class='form-label'>医生工号（随机生成不可修改）</label>
        <input type='text' class='form-control' id='docID' name='docID' value='" . $doc_ID . "' readonly>
    </div>
    <div class='col-md-5'>
        <label for='docName' class='form-label'>医生姓名</label>
        <input type='text' class='form-control' id='docName' name='docName'>
    </div>
    <div class='col-md-5'>
        <label for='docPhone' class='form-label'>医生电话</label>
        <input type='text' class='form-control' id='docPhone' name='docPhone'>
    </div>
    <div class='col-10'>
        <label for='defaultPwd' class='form-label'>初始登录密码（默认密码，如若修改请通知到个人）</label>
        <input type='text' class='form-control' id='defaultPwd' value='Doc123456@' name='defaultPwd'>
    </div>
    <div class='mb-3'>
        <label for='docPhoto' class='form-label'>医生照片（推荐照片比例位1:1）</label>
        <input class='form-control' type='file' id='docPhoto' name='docPhoto'>
    </div>";

echo "
    <div class='col-md-6'>
        <label for='docMainType' class='form-label'>主职业类别</label>
        <select id='docMainType' class='form-select' name='docMainType'>
            <option value='none'>--请选择--</option>";

include("a_getDepartments.php");

echo "
        </select>
    </div>
    <div class='col-md-4'>
        <label for='docAccept' class='form-label'>是否直接开始接诊（必选）</label>
        <select id='docAccept' class='form-select' name='docAccept'>
            <option>--请选择--</option>
            <option value='1'>是</option>
            <option value='0'>否</option>
        </select>
    </div>
    <div class='col-12'>
        <button type='submit' class='btn btn-primary' onclick='return checkSubmit();'>确认添加信息</button>
    </div>
</form>";
